#!/usr/bin/env node

var fs = require('fs');
var path = require('path');

var rootDir = process.argv[2];

var deleteFolderRecursive = function(removePath) {
  if( fs.existsSync(removePath) ) {
    fs.readdirSync(removePath).forEach(function(file,index){
      var curPath = path.join(removePath, file);
      if(fs.lstatSync(curPath).isDirectory()) { // recurse
        deleteFolderRecursive(curPath);
      } else { // delete file
        fs.unlinkSync(curPath);
      }
    });
    fs.rmdirSync(removePath);
  }
};

if(rootDir) {
	var platforms = (process.env.CORDOVA_PLATFORMS ? process.env.CORDOVA_PLATFORMS.split(',') : []);
	var foldersToDelete = ['css', 'js', 'img', 'templates', 'lib'];

	for (var x=0; x<platforms.length; x++) {
	    try {
	      	var platform = platforms[x].trim().toLowerCase();
	      	var wwwPath;

	      	if (platform == 'android') {
		        wwwPath = path.join('platforms', platform, 'assets', 'www');
		    } else {
		        wwwPath = path.join('platforms', platform, 'www');
		    }

		    for (var i=0; i<foldersToDelete.length; ++i) {
		        var folder = foldersToDelete[i];
		        var pathToDelete = path.join(wwwPath, folder);

		        if (fs.existsSync(pathToDelete)) {
		          process.stdout.write('removing "'+ folder +'" folder: ' + pathToDelete + '\n');
		          deleteFolderRecursive(pathToDelete);
		        }
		    }

	    } catch(e) {
	      process.stdout.write(e);
	    }
	 }
}